Code
pacman::p_load(ggrepel, patchwork,
ggthemes, hrbrthemes,
tidyverse, ggiraph, plotly,
patchwork, DT) Andre Ong Jia Kang
April 26, 2025
May 1, 2025
exam_data$tooltip <- c(paste0( "Name = ", exam_data$ID,"\n Class = ", exam_data$CLASS))
p <- ggplot(data=exam_data, aes(x = MATHS)) +
geom_dotplot_interactive(aes(tooltip = exam_data$tooltip),
stackgroups = TRUE, binwidth = 1, method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)
girafe(
ggobj = p,width_svg = 8,height_svg = 8*0.618
)tooltip_css <- "background-color:white; #<<
font-style:bold; color:black;" #<<
p <- ggplot(data=exam_data, aes(x = MATHS)) +
geom_dotplot_interactive( aes(tooltip = ID), stackgroups = TRUE, binwidth = 1, method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)
girafe( ggobj = p, width_svg = 6, height_svg = 6*0.618,
options = list( #<<
opts_tooltip( #<<
css = tooltip_css)) #<<
) tooltip <- function(y, ymax, accuracy = .01) {
mean <- scales::number(y, accuracy = accuracy)
sem <- scales::number(ymax - y, accuracy = accuracy)
paste("Mean maths scores:", mean, "+/-", sem)
}
gg_point <- ggplot(data=exam_data,
aes(x = RACE),
) +
stat_summary(aes(y = MATHS, tooltip = after_stat(tooltip(y, ymax))),
fun.data = "mean_se", geom = GeomInteractiveCol, fill = "light blue") +
stat_summary(aes(y = MATHS), fun.data = mean_se, geom = "errorbar", width = 0.2, size = 0.2)
girafe(ggobj = gg_point, width_svg = 8, height_svg = 8*0.618)p <- ggplot(data=exam_data, aes(x = MATHS)) +
geom_dotplot_interactive(aes(data_id = CLASS),stackgroups = TRUE,binwidth = 1, method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)
girafe(ggobj = p, width_svg = 6, height_svg = 6*0.618, options = list( opts_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity:0.2;")
)
) p <- ggplot(data=exam_data, aes(x = MATHS)) +
geom_dotplot_interactive( aes(data_id = CLASS), stackgroups = TRUE, binwidth = 1, method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)
girafe(
ggobj = p, width_svg = 6, height_svg = 6*0.618, options = list(
opts_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity:0.2;")
)
) exam_data$onclick <- sprintf("window.open(\"%s%s\")",
"https://www.moe.gov.sg/schoolfinder?journey=Primary%20school",
as.character(exam_data$ID))
p <- ggplot(data=exam_data, aes(x = MATHS)) + geom_dotplot_interactive(
aes(onclick = onclick), stackgroups = TRUE, binwidth = 1,method = "histodot") + scale_y_continuous(NULL,
breaks = NULL)
girafe(ggobj = p, width_svg = 6,height_svg = 6*0.618)p1 <- ggplot(data=exam_data,
aes(x = MATHS)) + geom_dotplot_interactive(aes(data_id = ID),
stackgroups = TRUE,binwidth = 1, method = "histodot") +
coord_cartesian(xlim=c(0,100)) +
scale_y_continuous(NULL,breaks = NULL)
p2 <- ggplot(data=exam_data, aes(x = ENGLISH)) +
geom_dotplot_interactive(aes(data_id = ID),stackgroups = TRUE, binwidth = 1, method = "histodot") +
coord_cartesian(xlim=c(0,100)) +
scale_y_continuous(NULL, breaks = NULL)
girafe(code = print(p1 + p2), width_svg = 6, height_svg = 3,
options = list( opts_hover(css = "fill: #202020;"),opts_hover_inv(css = "opacity:0.2;"))) d <- highlight_key(exam_data)
p1 <- ggplot(data=d, aes(x = MATHS,y = ENGLISH)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100), ylim=c(0,100))
p2 <- ggplot(data=d, aes(x = MATHS, y = SCIENCE)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100), ylim=c(0,100))
subplot(ggplotly(p1),
ggplotly(p2))# Scatter plot
scatter_plot <- ggplot(data = exam_data,
aes(x = MATHS, y = ENGLISH)) +
geom_point(alpha = 0.8) +
theme_minimal() +
labs(title = "Scatter Plot with Marginal Density Plots",
x = "Maths Score",
y = "English Score")
# Marginal density plot for x-axis (MATHS)
density_x <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_density(fill = "gray", alpha = 0.6) +
theme_minimal() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
# Marginal density plot for y-axis (ENGLISH)
density_y <- ggplot(data = exam_data,
aes(x = ENGLISH)) +
geom_density(fill = "gray", alpha = 0.6) +
coord_flip() + # Flip to make it vertical
theme_minimal() +
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# Combine plots using patchwork
combined_plot <- density_x +
plot_spacer() + # Empty space
scatter_plot +
density_y +
plot_layout(ncol = 2, widths = c(4, 1), heights = c(1, 4))
# Display the combined plot
combined_plot
# Step 1: Create scatter plot
scatter_plot <- ggplot(data = exam_data,
aes(x = MATHS, y = ENGLISH)) +
geom_point(alpha = 0.8) +
theme_minimal() +
labs(title = "Scatter Plot with Marginal Density Plots",
x = "Maths Score",
y = "English Score")
# Step 2: Add marginal density plots
ggMarginal(scatter_plot,
type = "density",
fill = "gray",
alpha = 0.6,
color = "black")
# Step 1: Create scatter plot
scatter_plot <- ggplot(data = exam_data,
aes(x = MATHS, y = ENGLISH)) +
geom_point(alpha = 0.8) +
theme_minimal() +
labs(title = "Scatter Plot with Marginal Boxplots",
x = "Maths Score",
y = "English Score")
# Step 2: Add marginal boxplots
ggMarginal(scatter_plot,
type ="boxplot",
color = "black")
# Step 1: Create scatter plot
scatter_plot <- ggplot(data = exam_data,
aes(x = MATHS, y = ENGLISH)) +
geom_point(alpha = 0.8) +
theme_minimal() +
labs(title = "Scatter Plot with Marginal Histogram Plots",
x = "Maths Score",
y = "English Score")
# Step 2: Add marginal histogram plots
ggMarginal(scatter_plot,
type = "histogram",
color = "black")
# Scatter plot
scatter_plot <- ggplot(data = exam_data,
aes(x = MATHS,
y = ENGLISH,
text = paste("Student", ID,
"<br>Maths: ", MATHS,
"<br>English: ", ENGLISH))) +
geom_point(alpha = 0.8) +
theme_minimal() +
labs(title = "Scatter Plot with Marginal Density Plots",
x = "Maths Score",
y = "English Score")
# Marginal density plot for x-axis (MATHS)
density_x <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_density(fill = "gray", alpha = 0.6) +
theme_minimal() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
# Marginal density plot for y-axis (ENGLISH)
density_y <- ggplot(data = exam_data,
aes(x = ENGLISH)) +
geom_density(fill = "gray", alpha = 0.6) +
coord_flip() + # Flip to make it vertical
theme_minimal() +
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
interactive_scatter <- ggplotly(scatter_plot, tooltip = "text")
# Convert marginal plots to plotly
interactive_x_density <- ggplotly(density_x) %>% hide_legend()
interactive_y_density <- ggplotly(density_y) %>% hide_legend()
# Step 3: Arrange all plots together using subplot
final_plot <- subplot(
interactive_x_density,
plot_spacer(),
interactive_scatter,
interactive_y_density,
nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2),
shareX = TRUE, shareY = TRUE
)
final_plotggplot(globalPop, aes(x = Old, y = Young, size = Population, colour = Country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}', x = '% Aged', y = '% Young') +
transition_time(Year) +
ease_aes('linear') 
gg <- ggplot(globalPop,
aes(x = Old,
y = Young,
size = Population,
colour = Country)) +
geom_point(aes(size = Population,
frame = Year),
alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(x = '% Aged',
y = '% Young') +
theme(legend.position='none')
ggplotly(gg)